package cate.game.role.farm.tower;

import cate.common.table.d.*;
import cate.common.table.farm.tower.row.TowerMainRow;
import cate.game.event.tower.TowerPassedEvent;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.farm.tower.msg.TowerChallengeResultResp;
import cate.game.role.farm.tower.msg.TowerDataResp;

public class TowerFTC extends AbstractFarmFTC<Tower> {
    /** 当前挑战美食神殿关卡的模板 */
    private TowerMainRow tpl;

    public TowerFTC(Role master, TowerMainRow tpl) {
        setup(master);
        this.tpl = tpl;
    }

    @Override
    protected Tower po(Role role) {
        return role.getFarm().tower;
    }

    @Override
    public void handleResult(Role role, PlayPO play) {
        super.handleResult(role, play);
        Tower tower = po(role);
        if(tower == null){
            return;
        }
        boolean win = play.winner == GDFight.Team.A;
        MixRes reward = new MixRes();
        if (win) {
            tower.todayPassed ++;
            boolean firstPass = tower.passedFloor + 1 == this.tpl.floor;
            if(this.tpl.floor > tower.passedFloor){
                tower.passedFloor = this.tpl.floor;
                publishEvent(role, new TowerPassedEvent(role, tower.passedFloor));
            }
            reward = new MixRes(tpl.rewardStr);
            if (firstPass) {
                reward.addList(new MixRes(tpl.rewardFirstExtStr));
            }
            long power = role.getGame().power.calcPositionPower(role, GDFunc.TOWER);
            role.getGame().rank.tower.record(role, this.tpl.floor, power);
        }
        reward.add(role, true, GDOperation.TOWER_CHALLENGE, tower.passedFloor);
        role.sendNow(new TowerDataResp(tower));
        role.sendNow(new TowerChallengeResultResp(win, reward));
    }
}
